<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionTestAjax()
	{
		echo CJSON::encode('测试ajax正确！');
		
	}
	
	/**
	 * 根据图书馆对图书四角编码的规范，给指定的字符串进行四角编码查询。
	 * 如果字库中不存在该汉字，不知如何进行错误信息传递及处理。
	 * @param string $letter	要进行四角编码的字符串。
	 */
	public function actionGetFcode($letter){
		// 		echo '测试用字：'.$letter ."<br/>";
		// $_letter0 = trim($letter,' ');
		// echo 'trimed：'.$_letter0 ."<br/>";
		$_result= null;
	
		if($letter == null)
		{
			$_result ='未正确传入字符！';
			echo CJSON::encode($_result);
			return;
		}
	
		//去除空格，并截取前3个汉字（如果存在的话）；
		$_letter0= str_replace(' ', '',$letter);
		$_letter1= substr($_letter0, 0,3);
		$_letter2= substr($_letter0, 3,3);
		$_letter3= substr($_letter0, 6,3);
	
		// 		echo 'str_replace：'.$_letter0 ."<br/>";
		// 		echo 'strlen:'. strlen($_letter0) ."<br/>";
		// 		echo 'substred1：'.$_letter1 ."<br/>";
		// 		echo 'substred2：'.$_letter2 ."<br/>";
		// 		echo 'substred3：'.$_letter3 ."<br/>";
	
	
		$_result= null;
		$_strlen= strlen($_letter0);
		if($_strlen == 3)//1个汉字
		{
			$model1 =  Fcode::model()->findByPk($_letter1);
			if($model1== null ) {
				$_result ='未传入正确字符:'.$_letter1;
				echo CJSON::encode($_result);
				return;
			}
	
			//	echo '分别：'.$_letter1 . $model1->fcode;
	
			$_result = $model1->fcode;
		}
		else if($_strlen == 6)//2个汉字
		{
			$model1 =  Fcode::model()->findByPk($_letter1);
			$model2 =  Fcode::model()->findByPk($_letter2);
			if($model1 ==null)
			{
				$_result ='找不到汉字[' .$_letter1 .']的四角编码，请联系管理员进行维护！';
				echo CJSON::encode($_result);
				return;
			}
			if($model2== null)
			{
				$_result ='找不到汉字[' .$_letter2.']的四角编码，请联系管理员进行维护！';
				echo CJSON::encode($_result);
				return;
			}
	
			// 			echo '分别：'.$_letter1 . $model1->fcode .$_letter2 . $model2->fcode;
	
			$_result =substr( $model1->fcode, 0,2) . substr( $model2->fcode, 0,2);
		}
		else{
			//3个以上
			$model1 =  Fcode::model()->findByPk($_letter1);
			$model2 =  Fcode::model()->findByPk($_letter2);
			$model3 =  Fcode::model()->findByPk($_letter3);
	
	
			if($model1 ==null)
			{
				$_result ='找不到汉字[' .$_letter1 .']的四角编码，请联系管理员进行维护！';
				echo CJSON::encode($_result);
				return;
			}
			if($model2== null)
			{
				$_result ='找不到汉字[' .$_letter2.']的四角编码，请联系管理员进行维护！';
				echo CJSON::encode($_result);
				return;
			}
			if($model3 ==null)
			{
				$_result ='找不到汉字[' .$_letter3.']的四角编码，请联系管理员进行维护！';
				echo CJSON::encode($_result);
				return;
			}
	
			// 			echo '分别：'.$_letter1 . $model1->fcode .$_letter2 . $model2->fcode .$_letter3 . $model3->fcode .'<br/>';
	
			$_result =substr( $model1->fcode, 0,2) . substr( $model2->fcode, 0,1). substr( $model3->fcode, 0,1);
		}
	
		echo CJSON::encode($_result);
		// 		if($model1 != null)
		// 			echo '四角编码：'.$_result ;
	}

}